define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, GoPage, $timeout, Distributor_creditrepayview, $stateParams, GoDialog, $window) {
		$scope.input = new Object();
		$scope.scope = new Object();
		$scope.id = $stateParams.id;
		$scope.scope.start_time = '';
		$scope.scope.end_time = '';
		$scope.end_time = '';
		$scope.start_time = '';
		$scope.count = null;
		$scope.isLoad = false;
		
		$scope.$on('$ionicView.afterEnter', function(){
			var lan = 0; //这是状态栏，只有ios的APP才有
			if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) { lan = 20; }
			var all = $window.innerHeight;
			var discredit_top1 = document.getElementById("discredit-top1").clientHeight;
			var discredit_top2 = document.getElementById("discredit-top2").clientHeight;
			$scope.discredit_myObj = { //滚动条可视范围
				"height": all - 44 - lan - discredit_top1 - discredit_top2 - 50 + "px"
			};
			$scope.init_data();
		},false)
		
		$scope.init_data = function() {
			Distributor_creditrepayview.distributor_credit(1, $scope.id, $scope.start_time, $scope.end_time, function(data) {
				if(data != null) {
					$scope.data = data;
				} else {
					$scope.data = new Object();
					$scope.data.now_debt = 0;
					$scope.data.credit_line = 0;
				}
				$scope.input.nPage = 1;
			})
		}
		
		//确认还款
		$scope.confrim = function(member_id, info_id, money, cancel) {
			Distributor_creditrepayview.confrim_credit(member_id, info_id, money, cancel, function(data) {
				if(data.success == true) {
					if(cancel==1)
						GoDialog.toast("作废成功");
					else
						GoDialog.toast("收款成功");
					$scope.init_data();
				} else{
					GoDialog.alert("提示",data.msg);
				}
			})
		}
		
		$scope.search = function() {
			if($scope.scope.start_time != '')
				$scope.start_time = (new Date($scope.scope.start_time)).getTime() / 1000;
			if($scope.scope.end_time != '')
				$scope.end_time = (new Date($scope.scope.end_time)).getTime() / 1000;
			Distributor_creditrepayview.distributor_credit(1, $scope.id, $scope.start_time, $scope.end_time, function(data) {
				if(data != null) {
					$scope.data = data;
				} else {
					$scope.data = new Object();
					$scope.data.now_debt = 0;
					$scope.data.credit_line = 0;
				}
				$scope.input.nPage = 1;
			})
		}
		
		$scope.doRefresh = function() {
			//防止重复加载
			$scope.isLoad = true;
			Distributor_creditrepayview.distributor_credit(1, $scope.id, $scope.start_time, $scope.end_time, function(data) {
				if(data != null) {
					$scope.data = data;
				} else {
					$scope.data = new Object();
					$scope.data.now_debt = 0;
					$scope.data.credit_line = 0;
				}
				$scope.input.nPage = 1;
			}, function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false; //可以加载
				}, 1500);
			});
		}
		$scope.doRefreshMore = function() {
			//防止重复加载
			$scope.isLoad = true;
			if($scope.data.credit == undefined) {
				$scope.input.nPage = 0;
			} else
			if($scope.data.page.count <= $scope.data.credit.length) {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				if($scope.data.page.count<$scope.input.nPage*10){
					$scope.input.nPage -=1;
					$scope.count = $scope.input.nPage *10;
				}else{
					$scope.count = null;
				}
			}

			Distributor_creditrepayview.distributor_credit($scope.input.nPage + 1, $scope.id, $scope.start_time, $scope.end_time, function(data) {
				if(data.credit.length > 0) {
					if($scope.data.credit != undefined) {
						if($scope.count!=null)
						$scope.data.credit.splice($scope.count,$scope.data.page.count-$scope.count);
						$scope.data.credit = $scope.data.credit.concat(data.credit);
						$scope.input.nPage += 1;
					} else {
						$scope.data = data;
						$scope.input.nPage = 1;
					}
				}
			}, function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false; //可以加载
				}, 1500);
			});
		}
		
		//查看凭证
		$scope.showBigImg = function(imgUrl) {
			GoDialog.showBigImg(imgUrl);
		}
		
		//点击其他区域关闭弹出窗       
		var htmlEl = angular.element(document.querySelector('html'));
		htmlEl.on('click', function(event) {　　
			if(event.target.nodeName === 'HTML') {　　　　
				if(my_voucher) {　　　　　　
					my_voucher.close();　　　　
				}　　　　　
			}
		});
		$scope.get_status = function (status){
			if(status==0){
				return ' 未确认';
			}else if(status==1){
				return ' 已确认';
			}else
			return ' 已作废';
		}
		
		
	}

	controller.$inject = ['$scope', 'GoPage', '$timeout', 'Distributor_creditrepayview', '$stateParams', 'GoDialog', '$window'];

	return app.registerController("distributor_creditrepayviewctrl",controller);
});